﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class BiaoDuanBianJi : BaseForm
    {

        private DataGridViewRow m_SelectRow = null;
        private int m_CurrentIndex = -1;

        public BiaoDuanBianJi(int Index)
        {
            InitializeComponent();
            m_CurrentIndex = Index;
        }
        public BiaoDuanBianJi(DataGridViewRow SelectRow,int Index)
        {
            InitializeComponent();
            m_SelectRow = SelectRow;
            m_CurrentIndex = Index;
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            if (m_SelectRow != null&&m_CurrentIndex!=tabControl1.SelectedIndex)
            {
                KeaoUtils.ShowInfoMessage("保存信息失败，请检查重试。");
                return;
            }

            if (tabControl1.SelectedTab.Text=="路基")
            {
                if (string.IsNullOrEmpty(txt_gongcheng_luji.Text))
                {
                    KeaoUtils.ShowInfoMessage("分项工程名称不能为空。");
                    return;
                }
                if (string.IsNullOrEmpty(txt_gonglihao1_luji.Text)||string.IsNullOrEmpty(txt_gonglihao2_luji.Text)||string.IsNullOrEmpty(txt_dian1_luji.Text)||string.IsNullOrEmpty(txt_dian2_luji.Text))
                {
                    KeaoUtils.ShowInfoMessage("桩号不能为空。");
                    return;
                }
                if (!KeaoUtils.isNumber(txt_gonglihao1_luji.Text) || !KeaoUtils.isNumber(txt_gonglihao2_luji.Text) || !KeaoUtils.isNumber(txt_dian1_luji.Text) || !KeaoUtils.isNumber(txt_dian2_luji.Text))
                {
                    KeaoUtils.ShowInfoMessage("桩号应该是数字。");
                    return;
                }
                decimal biaoDuanChangDu = 0m;
                decimal biaoDuanKuanDu = 0m;
                decimal biaoDuanHouDu = 0m;
                decimal ganmidu = 0m;
                decimal jihualiang = 0m;
                if (!string.IsNullOrEmpty(txt_biaoduankuandu.Text) && KeaoUtils.isNumber(txt_biaoduankuandu.Text))
                {
                    biaoDuanKuanDu = decimal.Parse(txt_biaoduankuandu.Text);
                }
                if (!string.IsNullOrEmpty(txt_biaoduanchangdu.Text) && KeaoUtils.isNumber(txt_biaoduanchangdu.Text))
                {
                    biaoDuanChangDu = decimal.Parse(txt_biaoduanchangdu.Text);
                }
                if (!string.IsNullOrEmpty(txt_biaoduanhoudu.Text) && KeaoUtils.isNumber(txt_biaoduanhoudu.Text))
                {
                    biaoDuanHouDu = decimal.Parse(txt_biaoduanhoudu.Text);
                }
                if (!string.IsNullOrEmpty(txt_ganmidu_luji.Text) && KeaoUtils.isNumber(txt_ganmidu_luji.Text))
                {
                    ganmidu = decimal.Parse(txt_ganmidu_luji.Text);
                }
                jihualiang = biaoDuanChangDu * biaoDuanKuanDu * biaoDuanHouDu * ganmidu;

                double gongLiHao1 = double.Parse(txt_gonglihao1_luji.Text) * 1000;
                double gongLiHao2 = double.Parse(txt_gonglihao2_luji.Text) * 1000;
                double dian1 = double.Parse(txt_dian1_luji.Text);
                double dian2 = double.Parse(txt_dian2_luji.Text);

                if (m_SelectRow == null)
                {
                    TableRow newRow = new TableRow("tbl_biaoduan", KeaoUtils.GetGUID(), DBCtl.插入);
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduanmingcheng", ValueMember = string.Format("{4}{0}{1}-{4}{2}{3}", txt_gonglihao1_luji.Text, txt_dian1_luji.Text.StartsWith("-") ? txt_dian1_luji.Text : "+" + txt_dian1_luji.Text, txt_gonglihao2_luji.Text, txt_dian2_luji.Text.StartsWith("-") ? txt_dian2_luji.Text : "+" + txt_dian2_luji.Text,cmb_K1.Text), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduanchangdu", ValueMember = biaoDuanChangDu.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduankuandu", ValueMember = biaoDuanKuanDu.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduanhoudu", ValueMember = biaoDuanHouDu.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduanleixing", ValueMember = "路基", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "zuoyoufu", ValueMember = cmb_luji_zuoyoufu.Text , ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "gonglihao1", ValueMember = gongLiHao1+dian1, ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "gonglihao2", ValueMember = gongLiHao2+dian2, ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName="riqi", ValueMember=date_riqi_luji.Value, ValueType= DataValueCommonType.日期时间 });
                    newRow.DataCols.Add(new TableCol() { ColName="fangliang", ValueMember=(biaoDuanChangDu*biaoDuanHouDu*biaoDuanKuanDu).ToString("0.00"), ValueType= DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName="ganmidu", ValueMember=ganmidu, ValueType= DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName="gongcheng", ValueMember= txt_gongcheng_luji.Text, ValueType= DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName="jihualiang", ValueMember=jihualiang.ToString("0.00"), ValueType= DataValueCommonType.数字 });
                    if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    TableRow editRow = new TableRow("tbl_biaoduan", string.Format("{0}", m_SelectRow.Cells["col_id"].Value), DBCtl.修改);
                    editRow.DataCols.Add(new TableCol() { ColName = "biaoduanmingcheng", ValueMember = string.Format("{4}{0}{1}-{4}{2}{3}", txt_gonglihao1_luji.Text, txt_dian1_luji.Text.StartsWith("-") ? txt_dian1_luji.Text : "+" + txt_dian1_luji.Text, txt_gonglihao2_luji.Text, txt_dian2_luji.Text.StartsWith("-") ? txt_dian2_luji.Text : "+" + txt_dian2_luji.Text,cmb_K1.Text), ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "biaoduanchangdu", ValueMember = biaoDuanChangDu.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    editRow.DataCols.Add(new TableCol() { ColName = "biaoduankuandu", ValueMember = biaoDuanKuanDu.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    editRow.DataCols.Add(new TableCol() { ColName = "biaoduanhoudu", ValueMember = biaoDuanHouDu.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    editRow.DataCols.Add(new TableCol() { ColName = "zuoyoufu", ValueMember = cmb_luji_zuoyoufu.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "gonglihao1", ValueMember = gongLiHao1+dian1, ValueType = DataValueCommonType.数字 });
                    editRow.DataCols.Add(new TableCol() { ColName = "gonglihao2", ValueMember = gongLiHao2+dian2, ValueType = DataValueCommonType.数字 });
                    editRow.DataCols.Add(new TableCol() { ColName = "riqi", ValueMember = date_riqi_luji.Value, ValueType = DataValueCommonType.日期时间 });
                    editRow.DataCols.Add(new TableCol() { ColName = "fangliang", ValueMember = (biaoDuanChangDu * biaoDuanHouDu * biaoDuanKuanDu).ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    editRow.DataCols.Add(new TableCol() { ColName = "ganmidu", ValueMember = ganmidu, ValueType = DataValueCommonType.数字 });
                    editRow.DataCols.Add(new TableCol() { ColName = "gongcheng", ValueMember = txt_gongcheng_luji.Text, ValueType = DataValueCommonType.字符串 });
                    editRow.DataCols.Add(new TableCol() { ColName = "jihualiang", ValueMember = jihualiang.ToString("0.00"), ValueType = DataValueCommonType.数字 });
                    if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
            }
            if (tabControl1.SelectedTab.Text=="其他")
            {
                if (string.IsNullOrEmpty(txt_mingcheng_qita.Text))
                {
                    KeaoUtils.ShowInfoMessage("去向名称不能为空。");
                    return;
                }
                if (m_SelectRow == null)
                {
                    TableRow newRow = new TableRow("tbl_biaoduan", Guid.NewGuid().ToString(), DBCtl.插入);
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduanmingcheng", ValueType = DataValueCommonType.字符串, ValueMember = txt_mingcheng_qita.Text });
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduanchangdu", ValueMember = 0, ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduankuandu", ValueMember = 0, ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduanhoudu", ValueMember = 0, ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "biaoduanleixing", ValueMember = "其他", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "zuoyoufu", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "gonglihao1", ValueMember = 0, ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "gonglihao2", ValueMember = 0, ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "riqi", ValueMember = DateTime.Today , ValueType = DataValueCommonType.日期时间 });
                    newRow.DataCols.Add(new TableCol() { ColName = "fangliang", ValueMember = 0, ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "ganmidu", ValueMember = 0, ValueType = DataValueCommonType.数字 });
                    newRow.DataCols.Add(new TableCol() { ColName = "gongcheng", ValueMember = "", ValueType = DataValueCommonType.字符串 });
                    newRow.DataCols.Add(new TableCol() { ColName = "jihualiang", ValueMember = 0, ValueType = DataValueCommonType.数字 });
                    if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    TableRow editRow = new TableRow("tbl_biaoduan", string.Format("{0}", m_SelectRow.Cells["col_id_qita"].Value), DBCtl.修改);
                    editRow.DataCols.Add(new TableCol() { ColName = "biaoduanmingcheng", ValueType = DataValueCommonType.字符串, ValueMember = txt_mingcheng_qita.Text });
                    if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                    {
                        DialogResult = DialogResult.OK;
                    }
                    
                }
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BiaoDuanBianJi_Load(object sender, EventArgs e)
        {
            try
            {
                tabControl1.SelectedIndex = m_CurrentIndex;
                date_riqi_luji.Value = DateTime.Today;
                cmb_luji_zuoyoufu.SelectedIndex = 0;
                cmb_K1.SelectedIndex = 0;
                
                BindCmb();

                if (m_SelectRow != null)
                {
                    if (tabControl1.SelectedTab.Text=="路基")
                    {
                        txt_biaoduanchangdu.Text = string.Format("{0}", m_SelectRow.Cells["col_biaoduanchangdu"].Value);
                        txt_biaoduankuandu.Text = string.Format("{0}", m_SelectRow.Cells["col_biaoduankuandu"].Value);
                        txt_biaoduanhoudu.Text = string.Format("{0}", m_SelectRow.Cells["col_biaoduanhoudu"].Value);
                        cmb_luji_zuoyoufu.SelectedItem = string.Format("{0}", m_SelectRow.Cells["col_zuoyoufu"].Value);
                        txt_gongcheng_luji.Text = string.Format("{0}", m_SelectRow.Cells["col_gongcheng_luji"].Value);
                        txt_ganmidu_luji.Text = string.Format("{0}", m_SelectRow.Cells["col_ganmidu_luji"].Value);

                        string gongLiHao1 = string.Format("{0}", m_SelectRow.Cells["col_gonglihao1_luji"].Value);
                        string gongLiHao2 = string.Format("{0}", m_SelectRow.Cells["col_gonglihao2_luji"].Value);

                        if (KeaoUtils.isNumber(gongLiHao1) && KeaoUtils.isNumber(gongLiHao2))
                        {
                            txt_dian1_luji.Text = (double.Parse(gongLiHao1) % 1000).ToString();
                            txt_dian2_luji.Text = (double.Parse(gongLiHao2) % 1000).ToString();
                            txt_gonglihao1_luji.Text = (Convert.ToInt32( double.Parse(gongLiHao1)) / 1000).ToString();
                            txt_gonglihao2_luji.Text = (Convert.ToInt32(double.Parse(gongLiHao2)) / 1000).ToString();
                        }
                    }
                    if (tabControl1.SelectedTab.Text=="其他")
                    {
                        txt_mingcheng_qita.Text = string.Format("{0}", m_SelectRow.Cells["col_mingcheng_qita"].Value);
                    }

                }
            }
            catch
            {
                return;
            }
        }

        private void BindCmb()
        {
            string createCondition = KeaoUtils._DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "路基", "", SearchConditionType.等于);
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian",createCondition);
            txt_gongcheng_luji.DisplayMember = "xiangmu";
            txt_gongcheng_luji.ValueMember = "xiangmu";
            txt_gongcheng_luji.DataSource = Dt;
        }

        private void txt_biaoduanchangdu_Click(object sender, EventArgs e)
        {
            TextBox t = sender as TextBox;
            t.Focus();
            t.SelectAll();
        }

        private void txt_gonglihao1_luji_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(txt_gonglihao1_luji.Text) && !string.IsNullOrEmpty(txt_gonglihao2_luji.Text) && !string.IsNullOrEmpty(txt_dian1_luji.Text) && !string.IsNullOrEmpty(txt_dian2_luji.Text))
                {
                    txt_biaoduanchangdu.Text = ((double.Parse(txt_gonglihao2_luji.Text) - double.Parse(txt_gonglihao1_luji.Text)) * 1000 + Math.Abs(double.Parse(txt_dian2_luji.Text)) - Math.Abs(double.Parse(txt_dian1_luji.Text))).ToString("0.00");

                }
            }
            catch
            {
                return;
            }
        }

    }
}
